package com.sinodata.ns.cell;

import com.sinodata.ns.sv.ISupervisor;
import com.sinodata.ns.sv.SupervisorManager;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleManager;
import jt.classic.system.object.ObjectManager;
import jt.classic.system.purview.PurviewManager;
import jt.classic.system.user.IUser;

import org.limp.mine.DataTrimmerI.DataItemList;

import java.sql.Connection;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 设计单元/标段管理者
 * @author mido
 *
 */
public class CellManager extends SimpleManager {
    public CellManager(Connection conn) {
        super(conn);
    }

    /**
     * 获取设计单元标段信息
     * <p>
     *    {Map,Map...}
     * </p>
     * @param cellId 设计单元ID
     * @return List
     */
    public List getCellBD(String cellId) {
        String sql = "SELECT * FROM APP_NS_CELLBD WHERE C_ID = ?";
        DataItemList dil = this.trimmerI.createDIL();
        dil.set(cellId);

        List list = this.trimmerI.searchMultiData(sql, dil);

        return list;
    }

    /**
     * 获取设计单元信息
     * @param bdid 标段ID
     * @return CellInfo
     */
    public CellInfo getCellByBDId(String bdid) {
        String sql = "SELECT C_ID FROM APP_NS_CELLBD WHERE CB_ID = ?";
        DataItemList dil = this.trimmerI.createDIL();
        dil.set(bdid);

        Map info = this.trimmerI.searchSingleData(sql, dil);

        CellInfo ci = null;

        if (info != null) {
            ci = this.getCellById((String) info.get("C_ID"));
        }

        return ci;
    }

    /**
     * 获取设计单元
     * @param id 设计单元ID
     * @return CellInfo
     */
    public CellInfo getCellById(String id) {
        String sql = "SELECT * FROM APP_NS_CELL WHERE C_ID = ?";
        DataItemList dil = this.trimmerI.createDIL();
        dil.set(id);

        Map info = this.trimmerI.searchSingleData(sql, dil);

        CellInfo ci = null;

        if (info != null) {
            ci = new CellInfo();

            sql = "SELECT * FROM APP_NS_CELLBD WHERE C_ID = ?";

            DataItemList dil1 = this.trimmerI.createDIL();
            dil1.set(id);

            List list = this.trimmerI.searchMultiData(sql, dil1);

            if ((list != null) && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    Map temp = (Map) list.get(i);

                    CellBDInfo cbi = new CellBDInfo(ci);

                    cbi.reload(temp);

                    list.set(i, cbi);
                }

                info.put("CELLBD", list);
            }

            ci.reload(info);
        }

        return ci;
    }

    /**
    * 获取设计单元
    * @param id 设计单元ID
    * @param user 用户
    * @return CellInfo
    */
    public CellInfo getCellById(String id, IUser user) {
        String sql = "SELECT * FROM APP_NS_CELL WHERE C_ID = ?";
        DataItemList dil = this.trimmerI.createDIL();
        dil.set(id);

        Map info = this.trimmerI.searchSingleData(sql, dil);

        CellInfo ci = null;

        if (info != null) {
            ci = new CellInfo();

            sql = "SELECT * FROM APP_NS_CELLBD WHERE C_ID = ?";

            DataItemList dil1 = this.trimmerI.createDIL();
            dil1.set(id);

            List list = this.trimmerI.searchMultiData(sql, dil1);

            List resulstList = new ArrayList();

            if ((list != null) && !list.isEmpty()) {
                PurviewManager pm = new PurviewManager(this.conn);

                for (int i = 0; i < list.size(); i++) {
                    Map temp = (Map) list.get(i);

                    CellBDInfo cbi = new CellBDInfo(ci);
                    cbi.reload(temp);

                    if (pm.negative(cbi.id(), "cell", user) > -1) {
                    	temp.put("purview", "0");
                    }else{
                    	temp.put("purview", "-1");
                    }
                    
                    resulstList.add(cbi);
                }

                info.put("CELLBD", resulstList);
            }

            ci.reload(info);
        }

        return ci;
    }

    /**
     * 获取标段监理
     * @param bdid
     * @return ISupervisor
     */
    public ISupervisor getSupervisorByBDId(String bdid) {
        String sql = "SELECT * FROM APP_NS_JLBD WHERE NU_BDID = ?";

        DataItemList dil = this.trimmerI.createDIL();
        dil.set(bdid);

        Map info = this.trimmerI.searchSingleData(sql, dil);

        ISupervisor sv = null;

        if (info != null) {
            String id = (String) info.get("NU_ID");

            SupervisorManager sm = new SupervisorManager(conn);

            try {
                sv = sm.getSupervisorById(id);
            } catch (Exception ex) {
                ISystem.catchException(ex);
            }
        }

        return sv;
    }

    /**
     * 获取标段监理(用户经授权可访问的)
     * @param user 用户
     * @return List
     */
    public List getCellByUser(IUser user) {
        String purviewSQL = ObjectManager.getObjectPurviewSQL("cell", user, 0,
                null);

        String sql =
            "SELECT APP_NS_CELL.C_ID FROM APP_NS_CELL,(SELECT APP_NS_CELLBD.C_ID FROM APP_NS_CELLBD, (" +
            purviewSQL +
            ") A WHERE APP_NS_CELLBD.CB_ID = A.OBJ_ID GROUP BY APP_NS_CELLBD.C_ID) A " +
            "WHERE APP_NS_CELL.C_ID = A.C_ID";

        List list = this.trimmer.searchMultiData(sql);

        if ((list != null) && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                Map info = (Map) list.get(i);
                String id = (String) info.get("C_ID");

                CellInfo cell = getCellById(id, user);

                list.set(i, cell);
            }
        }

        return list;
    }
}
